/**
 *  GTUG Zaragoza
 *  http://zaragoza.gtugs.org/
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.gtugs.zaragoza.feeds;

import java.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentValues;
/**
 * 
 * @author francho
 *
 */
public class AtomNewMessagesHandler extends DefaultHandler {
//
//	<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
//	<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?> 
//	<feed xmlns="http://www.w3.org/2005/Atom"> 
//	  <id>http://groups.google.com/group/zaragoza-gtug</id> 
//	  <title type="text">Zaragoza GTUG Google Group</title> 
//	  <subtitle type="text"> 
//	  Este es el grupo de usuarios de tecnologias de Google de la región de Zaragoza.
//	  </subtitle> 
//	  <link href="/group/zaragoza-gtug/feed/atom_v1_0_msgs.xml" rel="self" title="Zaragoza GTUG feed"/> 
//	  <updated>-0-0T::Z</updated> 
//	  <generator uri="http://groups.google.com" version="1.99">Google Groups</generator> 
//	</feed> 
	
	private boolean inFeed = false;
	private boolean inId = false;
	private boolean inTitle = false;
	private boolean inSubtitle = false;
	private boolean inLink = false;
	private boolean inUpdated = false;
	private boolean inGenerator = false;

	private boolean inEntry = false;
	private boolean inAuthor = false;
	private boolean inName = false;
	private boolean inEmail = false;
	private boolean inSummary = false;
	
	private ArrayList<ContentValues> mEntries =  new ArrayList<ContentValues>();
	private ContentValues mCurrentEntry;
	private DateFormat mDateFormat;
	
	public AtomNewMessagesHandler(){
		mDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");	
	}
	
	/**
	 * Receives notification of the beginning of an element
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		if(localName.equalsIgnoreCase("feed")) {
			inFeed = true;
			
			mEntries = new ArrayList<ContentValues>();
			
		} else if(localName.equalsIgnoreCase("id")) {
			inId = true;
		} else if(localName.equalsIgnoreCase("title")) {
			inTitle = true;
		} else if(localName.equalsIgnoreCase("subtitle")) {
			inSubtitle = true;
		} else if(localName.equalsIgnoreCase("link")) {
			inLink = true;
			
    		if(inEntry && attributes != null) {
    			String thelink = attributes.getValue("href");
    			mCurrentEntry.put(FeedEntry.LINK,attributes.getValue("href"));
    		} 
		} else if(localName.equalsIgnoreCase("updated")) {
			inUpdated = true;
		} else if(localName.equalsIgnoreCase("generator")) {
			inGenerator = true;
		} else if(localName.equalsIgnoreCase("entry")) {
			inEntry = true;
			
			mCurrentEntry = new ContentValues();
			
		} else if(localName.equalsIgnoreCase("author")) {
			inAuthor = true;
		} else if(localName.equalsIgnoreCase("name")) {
			inName = true;
		} else if(localName.equalsIgnoreCase("email")) {
			inEmail = true;
		} else if(localName.equalsIgnoreCase("summary")) {
			inSummary = true;
		}	
	}

	/**
	 * Receives notification of the ending of an element
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equalsIgnoreCase("feed")) {
			inFeed = false;
		} else if(localName.equalsIgnoreCase("id")) {
			inId = false;
		} else if(localName.equalsIgnoreCase("title")) {
			inTitle = false;
		} else if(localName.equalsIgnoreCase("subtitle")) {
			inSubtitle = false;
		} else if(localName.equalsIgnoreCase("link")) {
			inLink = false;
		} else if(localName.equalsIgnoreCase("updated")) {
			inUpdated = false;
		} else if(localName.equalsIgnoreCase("generator")) {
			inGenerator = false;
		} else if(localName.equalsIgnoreCase("entry")) {
			inEntry = false;
			
			mEntries.add(mCurrentEntry);
			
			mCurrentEntry = new ContentValues();
			
		} else if(localName.equalsIgnoreCase("author")) {
			inAuthor = false;
		} else if(localName.equalsIgnoreCase("name")) {
			inName = false;
		} else if(localName.equalsIgnoreCase("email")) {
			inEmail = false;
		} else if(localName.equalsIgnoreCase("summary")) {
			inSummary = false;
		}	
	}
	
	/** 
	 * Receives notification of the content of an element
     */
	@Override
	public void characters(char ch[], int start, int length) {
		String value = new String(ch, start, length);
		
		if (inEntry) {
			if (inAuthor && inName) {
				mCurrentEntry.put(FeedEntry.AUTHOR_NAME, value);
			} else if(inAuthor && inEmail) {
				mCurrentEntry.put(FeedEntry.AUTHOR_EMAIL, value);
			} else if (inUpdated) {
				try {
					long fecha = mDateFormat.parse(value).getTime();
					mCurrentEntry.put(FeedEntry.UPDATED, fecha);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else if(inId) {
				mCurrentEntry.put(FeedEntry.ID, value);
			} else if(inTitle) {
				if(mCurrentEntry.containsKey(FeedEntry.TITLE)) {
					StringBuffer title = new StringBuffer(mCurrentEntry.getAsString(FeedEntry.TITLE));
					title.append(value);
					mCurrentEntry.put(FeedEntry.TITLE, title.toString());
				} else { 
					mCurrentEntry.put(FeedEntry.TITLE, value);
				}
			} else if(inSummary) {
				if(mCurrentEntry.containsKey(FeedEntry.SUMMARY)) {
					StringBuffer summary = new StringBuffer(mCurrentEntry.getAsString(FeedEntry.SUMMARY));
					summary.append(value);
					mCurrentEntry.put(FeedEntry.SUMMARY, summary.toString());
				} else { 
					mCurrentEntry.put(FeedEntry.SUMMARY, value);
				}
			}
		} else {
			// TODO parsear cabecera
		}
	}

	/**
	 * 
	 * @return the parsed entries
	 */
	public ArrayList<ContentValues> getEntries() {
		return mEntries;
	}


	
	
//	@Override
//    public void startElement(String namespaceURI, String localName,
//                    String qName, Attributes atts) throws SAXException {
//    	// Nos vamos a centrar solo en los items
//    	if(localName.equalsIgnoreCase("item")) {
//    		in_item = true;
//    		rssItem = new ContentValues();
//    	}
	
}
